package com.clgc.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.clgc.baseservice.exception.MyException;
import com.clgc.eduservice.entity.EduStudent;
import com.clgc.eduservice.entity.excel.StudentExcel;
import com.clgc.eduservice.service.EduStudentService;
import com.clgc.utils.MD5Utils;
import com.clgc.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StudentListener extends AnalysisEventListener<StudentExcel> {
    @Autowired
    private EduStudentService eduStudentService;

    @Override
    public void invoke(StudentExcel studentExcel, AnalysisContext analysisContext) {
        System.out.println("------------读取学生表-----------");
        System.out.println("studentExcel-------->" + studentExcel);
        EduStudent eduStudent = new EduStudent();
        eduStudent.setName(studentExcel.getName());
        eduStudent.setSex(studentExcel.getSex());
        eduStudent.setMajorId(studentExcel.getMajorId());
        eduStudent.setClassId(studentExcel.getClassId());
        eduStudent.setUsername(studentExcel.getUsername());

        //密码md5加密
        String pwd = studentExcel.getPwd();
        String md5Pwd = MD5Utils.MD5Encode(pwd);
        eduStudent.setPwd(md5Pwd);

        eduStudent.setEmail(studentExcel.getEmail());

        //插入数据库
        eduStudentService.save(eduStudent);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
